<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态绘制多个点</title>
    <script src="glMatrix-0.9.6.min.js"></script>
    <script>
        let vertexstring = `
        attribute vec4 a_position;
        uniform     mat4    proj;
        void main(void){
            gl_Position = a_position;
            gl_PointSize=60.0;
        }
        `;
        let fragmentstring = `
        void main(void){
          gl_FragColor = vec4(0,0,1.0,1.0);
        }
        `;
        var projMat4 = mat4.create();
        var webgl;
        let webglDiv=null;
        var POINTS = [];

        function init() {//入口函数
            initWebgl();
            initShader();
            initBuffer();
            draw();
        }
        function initWebgl() {//webgl初始化函数
            webglDiv = document.getElementById('myCanvas');
            webgl = webglDiv.getContext("webgl");
            webgl.viewport(0, 0, webglDiv.clientWidth, webglDiv.clientHeight);
            mat4.ortho(0, webglDiv.clientWidth, webglDiv.clientHeight, 0, -1.0, 1.0, projMat4)
        }

        function initShader() {//shader初始化函数

            let vsshader = webgl.createShader(webgl.VERTEX_SHADER);
            let fsshader = webgl.createShader(webgl.FRAGMENT_SHADER);

            webgl.shaderSource(vsshader, vertexstring);
            webgl.shaderSource(fsshader, fragmentstring);

            webgl.compileShader(vsshader);
            webgl.compileShader(fsshader);
            if (!webgl.getShaderParameter(vsshader, webgl.COMPILE_STATUS)) {
                var err = webgl.getShaderInfoLog(vsshader);
                alert(err);
                return;
            }
            if (!webgl.getShaderParameter(fsshader, webgl.COMPILE_STATUS)) {
                var err = webgl.getShaderInfoLog(fsshader);
                alert(err);
                return;
            }
            let program = webgl.createProgram();
            webgl.attachShader(program, vsshader);
            webgl.attachShader(program, fsshader)

            webgl.linkProgram(program);
            webgl.useProgram(program);

            webgl.program = program
        }
        
        function initBuffer() {//数据存储的初始化函数
            let pointPosition = new Float32Array([100.0, 100.0, 0.0, 1.0]);
            let aPsotion = webgl.getAttribLocation(webgl.program, "a_position");
            
            // 添加鼠标点击事件
            document.addEventListener("mousedown", function (e) {
                let x = e.clientX;
                let y = e.clientY;
                let rect = e.target.getBoundingClientRect();
                // console.log(rect,'//');
                let pointX = ((x - rect.left) - rect.width/2) / (rect.width/2);
                let pointY = (rect.height/2 - (y - rect.top)) / (rect.height/2);
                POINTS.push(pointX);
                POINTS.push(pointY);
                POINTS.push(0);//z坐标
                POINTS.push(1.0);//vec4
              
                let pointPosition = new Float32Array(POINTS);//替换成当前的点坐标
                let pointBuffer = webgl.createBuffer();//创建缓存区
                webgl.bindBuffer(webgl.ARRAY_BUFFER, pointBuffer);//绑定缓存区
                webgl.bufferData(webgl.ARRAY_BUFFER, pointPosition, webgl.STATIC_DRAW)//给缓存区赋值;
                webgl.enableVertexAttribArray(aPsotion);//启用
                webgl.vertexAttribPointer(aPsotion, 4, webgl.FLOAT, false, 4*4, 0*4);

                webgl.clearColor(0.0, 0.0, 0.0, 1.0);
                webgl.clear(webgl.COLOR_BUFFER_BIT | webgl.DEPTH_BUFFER_BIT);
                webgl.drawArrays(webgl.POINTS, 0, POINTS.length);
            })
            
            webgl.vertexAttrib4fv(aPsotion, pointPosition);
            let uniformProj = webgl.getUniformLocation(webgl.program, "proj");
            webgl.uniformMatrix4fv(uniformProj, false, projMat4);
        }

        function draw() {//绘制
            webgl.clearColor(0.0, 0.0, 0.0, 1.0);
            // webgl.clear(webgl.COLOR_BUFFER_BIT | webgl.DEPTH_BUFFER_BIT);
            // webgl.drawArrays(webgl.POINTS, 0, 1);
        }
    </script>
</head>

<body onload="init()">
    <canvas id="myCanvas" width="800" height="800"></canvas>
</body>
</html>